package cn.kgc.vue.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.kgc.vue.anno.LogAnnotation;
import cn.kgc.vue.common.Operation;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.CoursePageDTO;
import cn.kgc.vue.entity.Course;
import cn.kgc.vue.entity.Student;
import cn.kgc.vue.service.CourseService;
import cn.kgc.vue.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 课工场
 * @since 2023/8/21
 * 课程控制层
 */
@RestController
@RequestMapping("course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentService studentService;

    @GetMapping("select")
    public ResponseResult getSelect() {
        ResponseResult responseResult = new ResponseResult();
        List<Course> list = courseService.list();
        responseResult.success(ResponseStatus.SUCCESS, list);
        return responseResult;
    }

    @GetMapping("page")
    public ResponseResult getPage(CoursePageDTO coursePageDTO) {

        return courseService.getPage(coursePageDTO);
    }


    @PostMapping("saveOrUpdate")
    @LogAnnotation(model = "课程模块",operation = Operation.ADD)
    @RequiresPermissions(value = {"base:course:add","base:course:update"},logical = Logical.OR)
    public ResponseResult addOrUpdate(@RequestBody Course course) {

        ResponseResult responseResult = new ResponseResult();
        //判定名称是否已经存在
        LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();


        lambda.eq(Course::getCourseNo, course.getCourseNo())
                .or()
                .eq(Course::getCourseName, course.getCourseName());

        List<Course> list = courseService.list(lambda);
        if (ObjectUtil.isNotEmpty(list)) {
            responseResult.setMsg("课程编号或或名称已存在");
            responseResult.setCode(500);
            return responseResult;
        }
        courseService.saveOrUpdate(course);
        responseResult.success();

        return responseResult;
    }

    @GetMapping("getById")
    public ResponseResult getCourse(Integer id) {
        Course course = courseService.getById(id);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS, course);
        return responseResult;
    }

    @DeleteMapping("delete")
    @LogAnnotation(model = "课程模块",operation = Operation.DELETE)
    @RequiresPermissions("base:course:delete")
    public ResponseResult deleteCourse(Integer id) {

        ResponseResult responseResult = new ResponseResult();
        // 判定课程是否在学生表中存在
        LambdaQueryWrapper<Student> lambda = new QueryWrapper<Student>().lambda();
        lambda.eq(Student::getCourseId, id);
        List<Student> list = studentService.list(lambda);
        if (ObjectUtil.isNotEmpty(list)) {
            responseResult.setCode(500);
            responseResult.setMsg("不能删除该课程");
            return responseResult;
        }
        courseService.removeById(id);
        responseResult.success();
        return responseResult;
    }


}
